// pages/active/active.js
const chooseLocation = requirePlugin('chooseLocation');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:'',
    longitude:'',
    speed:'',
    accuracy:'',
    name:'',
    address:'',
    province:'',
    city:'',
    district:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this.getLocal()
    this.WxGetLocal()
  },
  lookMap() {
    const latitude = parseFloat(this.data.latitude);
    console.log(latitude);
    const longitude = parseFloat(this.data.longitude);
    console.log(longitude);
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  },
  getLocal() {
    const key = 'FH2BZ-AYOH3-W3Q3D-R3YFK-NVIWK-J3FSI'; //使用在腾讯位置服务申请的key
    const referer = 'demo'; //调用插件的app的名称
    const location = JSON.stringify({
      latitude: this.data.latitude,
      longitude: this.data.longitude
    });
    const category = '生活服务,娱乐休闲';

    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
    }).then(res=>{
      console.log(res);
    }
    );
  },
  WxGetLocal(){
    const that = this
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        that.setData({
          latitude:latitude,
          longitude:longitude,
          speed:speed,
          accuracy:accuracy
        })
      }
     })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {
    const location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
    console.log(location);
    if(!location) {
      return
    }
    this.setData({
      latitude:location.latitude,
      longitude:location.longitude,
      name:location.name,
      address:location.address,
      accuracy:location.accuracy,
      province:location.province,
      city:location.city,
      district:location.district
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})